package com.sg.service.biz.app.nb;

import cn.hutool.core.bean.BeanUtil;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.biz.app.req.*;
import com.sg.dto.biz.app.res.*;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author ： 手工接入方法
 * @version 1.0
 * @since 2022/5/28 15:32
 */
@Service
public class NbCustomExecutionCycle {
    /**
     * code:receptionService
     * name:M3执行接收字段入参（特殊方法）
     * desc:undefined
     * gen by moon at 5/13/2023, 9:32:10 PM
     **/
    @Trace(operationName = "M3执行接收字段入参（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementAcceptFieldRespDto implementAcceptField(ImplementAcceptFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementAcceptFieldRespDto.class);
    }

    /**
     * code:receptionService
     * name:约定：是
     * desc:undefined
     * gen by moon at 5/14/2023, 3:01:30 AM
     **/
    @Trace(operationName = "约定：是")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public CalculateAcceptAboveFieldsRespDto calculateAcceptAboveFields(CalculateAcceptAboveFieldsReqDto reqDto) {
        return BeanUtil.toBean(reqDto, CalculateAcceptAboveFieldsRespDto.class);
    }

    /**
     * code:cycleShift
     * name:M3执行计算时段规则时间（特殊方法）
     * desc:undefined
     * gen by moon at 5/22/2023, 4:22:39 AMcycleShift
     **/
    @Trace(operationName = "M3执行计算时段规则时间（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementCountTimeRulesTimeRespDto implementCountTimeRulesTime(ImplementCountTimeRulesTimeReqDto reqDto) {
        ImplementCountTimeRulesTimeRespDto retData = new ImplementCountTimeRulesTimeRespDto();
        //计算基准时间
        Date baseDate = reqDto.getReferenceCycleTime();
        if (reqDto.getStartStopType().equals("LAST_DAY_CYCLE")) {
            //需要加一秒
            baseDate = CommonFunctionHelper.addDateBySec(baseDate, 1);
        }

        //计算汇报开始时间
        Date reportStartTime = new Date();
        Long sec = 0L;
        if (reqDto.getTimeUnit().equals("DAY")) {
            if (reqDto.getStartStopType().equals("LAST_DAY_CYCLE")) {
                //需要加一天
                reqDto.setTimeRelativeNumber(reqDto.getTimeRelativeNumber() + 1);
            }
            sec = reqDto.getTimeRelativeNumber() * 24 * 3600;
            if (reqDto.getIsContains().equals("TRUE")) {
                sec = sec - 24 * 3600;
            }
        } else if (reqDto.getTimeUnit().equals("HOUR")) {
            sec = reqDto.getTimeRelativeNumber() * 3600;
        } else if (reqDto.getTimeUnit().equals("MINUTES")) {
            sec = reqDto.getTimeRelativeNumber() * 60;
        }
        if (reqDto.getCalculateType().equals("FORWARD")) {
            sec = -sec;
        }
        reportStartTime = CommonFunctionHelper.addDateBySec(baseDate, sec.intValue());
        //计算汇报结束时间
        Long diffSec = 0L;
        if (reqDto.getTimeUnit().equals("DAY")) {
            diffSec = reqDto.getTimeNumber() * 24 * 3600;
        } else if (reqDto.getTimeUnit().equals("HOUR")) {
            diffSec = reqDto.getTimeNumber() * 3600;
        } else if (reqDto.getTimeUnit().equals("MINUTES")) {
            diffSec = reqDto.getTimeNumber() * 60;
        }
        Date reportEndTime = CommonFunctionHelper.addDateBySec(baseDate, sec.intValue() + diffSec.intValue() - 1);

        //计算评价开始时间
        Date evaStartTime = CommonFunctionHelper.addDateBySec(reportEndTime, 1);

        //计算评价结束时间
        Long diffSec2 = 0L;
        if (reqDto.getTimeUnit().equals("DAY")) {
            diffSec2 = reqDto.getFollowTimeNumber() * 24 * 3600;
        } else if (reqDto.getTimeUnit().equals("HOUR")) {
            diffSec2 = reqDto.getFollowTimeNumber() * 3600;
        } else if (reqDto.getTimeUnit().equals("MINUTES")) {
            diffSec2 = reqDto.getFollowTimeNumber() * 60;
        }
        Date evaEndTime = CommonFunctionHelper.addDateBySec(evaStartTime, diffSec2.intValue() - 1);

        //出参
        retData.setTimePeriodStartTime(reportStartTime);
        retData.setTimePeriodEndTime(reportEndTime);
        if (reqDto.getFollowTimeNumber() == null || reqDto.getFollowTimeNumber() == 0) {
            //如果跟随时段区间数量等于0，让前面的 评价时间 算了也白算
            retData.setFollowTimePeriodStartTime(null);
            retData.setFollowTimePeriodEndTime(null);
        } else {
            retData.setFollowTimePeriodStartTime(evaStartTime);
            retData.setFollowTimePeriodEndTime(evaEndTime);
        }
        return retData;
    }

    /**
     * code:getLastAndNextCycle
     * name:M3-获取获取当前周期下一个周期（特殊方法）
     * desc:undefined
     * gen by moon at 5/22/2023, 4:22:40 AM
     **/
    @Trace(operationName = "M3-获取获取当前周期下一个周期（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ObtainGetNextCycleRespDto obtainGetNextCycle(ObtainGetNextCycleReqDto reqDto) {
        ObtainGetNextCycleRespDto retData = new ObtainGetNextCycleRespDto();
        boolean bflag = false;
        ExecuteCycleStageDto last = null;
        for (ExecuteCycleStageDto one : reqDto.getExecuteCycleStageList()) {

            if (one.getCycleId().equals(reqDto.getCycleId()) && one.getCycleTypeCode().equals(reqDto.getCycleTypeCode())) {
                break;
            }
            last = one;

        }
        retData.setCycleId(last.getCycleId());
        return retData;
    }

    /**
     * code:receptionService
     * name:M3接收第二末级周期类型及周期模式类型编码
     * desc:undefined
     * gen by moon at 3/26/2024, 1:26:06 AM
     **/
    @Trace(operationName = "M3接收第二末级周期类型及周期模式类型编码")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementFiveReceivingFieldRespDto implementFiveReceivingField(ImplementFiveReceivingFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementFiveReceivingFieldRespDto.class);
    }

    /**
     * code:receptionService
     * name:M3接收执行周期阶段ID
     * desc:undefined
     * gen by moon at 4/15/2024, 1:36:53 AM
     **/
    @Trace(operationName = "M3接收执行周期阶段ID")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementSevenReceivingFieldRespDto implementSevenReceivingField(ImplementSevenReceivingFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementSevenReceivingFieldRespDto.class);
    }

    /**
     * code:receptionService
     * name:M3接收入参字段
     * desc:undefined
     * gen by moon at 4/28/2024, 2:34:09 PM
     **/
    @Trace(operationName = "M3接收入参字段")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementEightReceivingFieldRespDto implementEightReceivingField(ImplementEightReceivingFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementEightReceivingFieldRespDto.class);
    }

    /**
     * code:receptionService
     * name:M3接收周期开始时间出参
     * desc:undefined
     * gen by moon at 6/9/2024, 4:35:33 PM
     **/
    @Trace(operationName = "M3接收周期开始时间出参")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementElevenReceivingFieldRespDto implementElevenReceivingField(ImplementElevenReceivingFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementElevenReceivingFieldRespDto.class);

    }
    //手工接入方法
}
